var express = require('express');
var mysql = require('mysql');
var utils = require('../utils');
var router = express.Router();

var database = require('../database.config');
var connection = mysql.createConnection(database);
connection.connect();

router.post('/list', function (req, res, next) {
  const params = req.body;
  if (!params.chatCode) {
    res.send('缺少标识', 500);
    return;
  }
  var sql = `SELECT 
  f.id, f.userRemarks, u.chatCode, u.city, u.headImg, u.province, u.sex, u.telephone, u.userName, u.userProfile 
  FROM 
  chat_friend f LEFT JOIN chat_user u ON f.chatCode=u.chatCode WHERE f.user_chatCode='${params.chatCode}'`;
  connection.query(sql, function (error, result) {
    if (error) throw error;
    result.forEach(element => {
      element.base = 'http://' + req.host + ':' + global.port;
    });
    res.send(utils.sendData(result));
  });
});

router.post('/add', function (req, res, next) {
  const params = req.body;

  var sql = `INSERT INTO chat_friend (chatCode, userRemarks, user_chatCode) VALUES (?, ?, ?)`;
  var addSpl = [params.chatCode, null, params.userChatCode]
  connection.query(sql, addSpl, function (error, result) {
    if (error) throw error;

    var selectSql = `SELECT 
    f.id, f.userRemarks, f.user_chatCode userChatCode u.chatCode, u.city, u.headImg, u.province, u.sex, u.telephone, u.userName, u.userProfile 
    FROM 
    chat_friend f LEFT JOIN chat_user u ON f.chatCode=u.chatCode WHERE f.chatCode='${params.chatCode}' and f.user_chatCode='${params.userChatCode}' `;
    connection.query(selectSql, function (error, result) {
      if (error) throw error;
      result.forEach(element => {
        element.base = 'http://' + req.host + ':' + global.port;
      });
      res.send(utils.sendData(result[0]));
    });
  });
});

router.post('/apply', function (req, res, next) {
  const params = req.body;

  var selectSql = `select * from chat_friend where chatCode='${params.chatCode}' and user_chatCode='${params.userChatCode}'`
  connection.query(selectSql, function (error, result) {
    if (error) throw error;
    if (result.length > 0) {
      res.send(utils.sendData(null, 500, '已存在该好友'));
    } else {
      var sql = `INSERT INTO chat_friend_apply (chatCode, status, user_chatCode) VALUES (?, ?, ?)`;
      var addSpl = [params.chatCode, params.status, params.userChatCode]
      connection.query(sql, addSpl, function (err, result1) {
        if (err) throw err;
        res.send(utils.sendData(result1));
      });
    }
  });
});

router.post('/apply/list', function (req, res, next) {
  const params = req.body;
  var sql = `SELECT
	a.id,
	a.chatCode friendChatCode,
	a.status,
	a.user_chatCode myChatCode,
	u.telephone friendTelephone,
	u.userName friendUserName,
	u.sex friendSex,
	u.headImg friendHeadImg,
	u.userProfile friendUserProfile,
	u.province friendProvince,
	u.city friendCity,
	cu.telephone myTelephone,
	cu.userName myUserName,
	cu.sex mySex,
	cu.headImg myHeadImg,
	cu.userProfile myUserProfile,
	cu.province myProvince,
	cu.city myCity
FROM
	chat_friend_apply a
LEFT JOIN chat_user u ON a.chatCode = u.chatCode
LEFT JOIN chat_user cu ON a.user_chatCode=cu.chatCode
WHERE
	a.user_chatCode = '${params.userChatCode}' OR a.chatCode = '${params.userChatCode}' ORDER BY a.id DESC`;
  connection.query(sql, function (error, result) {
    if (error) throw error;
    result.forEach((element) => {
      element.base = 'http://' + req.host + ':' + global.port;
    })
    res.send(utils.sendData(result));
  });
});

router.post('/apply/update', function (req, res, next) {
  const params = req.body;
  var sql = `UPDATE chat_friend_apply SET STATUS=? WHERE id=?`;
  var updataSql = [params.status, params.id]
  connection.query(sql, updataSql, function (error, result) {
    if (error) throw error;
    res.send(utils.sendData(result, 0, '操作成功'));
  });
});

module.exports = router;
